#include "../include/User.h"
#include "../include/Resources.h"
#include <vector>
#include <iostream>
#include <string>
using namespace std;

User::User(int newId, Bank& newBank, double firstYitra): _id(newId), _res(vector<Resources>()), _account(new BankAccount::BankAccount(newId,firstYitra))
{

	for(int i=0; i<7; i++)
		_res.push_back(Resources::Resources(ass2::eEnergy(i)));
	newBank.addAccount(*_account);
}

void User::createEnergy(ass2::eEnergy type, int amount){

	for (int i=0; i<7; i++)
		if (_res.at(i).getType() == type){
			_res.at(i).create(amount);
		}

}

void User::consumeEnergy(ass2::eEnergy type, int amount){

	for (int i=0; i<7; i++)
		if (_res.at(i).getType() == type){
			_res.at(i).consume(amount);
		}
}

void User::sellEnergy(ass2::eEnergy type, int amount, double price){

	for (int i=0; i<7; i++)
		if (_res.at(i).getType() == type){
			_res.at(i).sell(amount);
		}
	_account->changeYitra((price*amount)*100.5/100);
}

void User::buyEnergy(ass2::eEnergy type, int amount, double price){
	for (int i=0; i<7; i++)
			if (_res.at(i).getType() == type){
				_res.at(i).buy(amount);
			}
		_account->changeYitra(-((price*amount)*100.5/100));
}

double User::getYitra(){
	return _account->getYitra();
}

double User::getProfit(){
	return _account->calcProfit();
}

void User::endOfDay(){
	_account->endOfDay();
}

int User::getId(){
	return _id;
}

Resources User::getResource(ass2::eEnergy type){

	Resources ans;
	for (int i=0; i<7; i++)
		if (_res.at(i).getType() == type)
			ans = _res.at(i);

	return ans;
}

User::~User()
{
	delete _account;
}
